// merchant//alliance/allianceMerList/index.js
const app = getApp();
const utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allianceId: 0,
    CurrentPage: 1,
    loadingMoreHidden: 1,
    keyword: '',
    staffList: [],
    showNumber: false,
    top: 150,
  },

  showMember: function(e){
    let merUid = e.currentTarget.dataset.meruid;
    wx.navigateTo({
      url: '../menber/index?allianceId=' + this.data.allianceId + '&meruid=' + merUid,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.allianceId){
      this.getAllianceStaffList(options.allianceId);
      this.setData({
        allianceId: options.allianceId
      })
    }
  },

  getAllianceStaffList: function(allianceId){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/alliance/GetApplyStaffList?allianceid='+ allianceId +'&nickname=' + that.data.keyword + '&page=' + that.data.CurrentPage,
      success: function(res){
        if(res.data.Code == 0){
          if(res.data.Data.Items.length > 0){
            let list = res.data.Data.Items;
            if (that.data.CurrentPage != 1) {
              var preGoods = that.data.staffList;
              list = preGoods.concat(list);
            }
            for(let i = 0; i < list.length; i++){
              list[i].Avater = utils.formatAvater(list[i].Avater, 'https://static.711688.net.cn/upload/card/source/');
            }
            that.setData({
              staffList: list,
              loadingMoreHidden: res.data.Data.TotalPages
            })
          }
        }
      }
    })
  },

  // 以下几个方法与搜索相关
  handleInput: function(e) {
    this.data.keyword = e.detail.value;
  },
  handleBlur: function(e) {
    this.data.keyword = e.detail.value;
  },
  handleConfirm: function(e) {
    this.data.keyword = e.detail.value;
    this.data.CurrentPage = 1;
    let allianceId = this.data.allianceId;
    this.getAllianceStaffList(allianceId);
  },
  handleSearchWord: function() {
    this.data.CurrentPage = 1;
    let allianceId = this.data.allianceId;
    this.getAllianceStaffList(allianceId);
  },

  showNumber: function(e) {
    let applyid = e.currentTarget.dataset.applyid;
    let singlecount = e.currentTarget.dataset.singlecount
    this.setData({
      showNumber: !this.data.showNumber,
      applyidLeader: applyid,
      leaderSingleCount: singlecount
    })
  },
  bindleaderSingleCount: function(e) {
    this.setData({
      leaderSingleCount: e.detail.value
    })
  },

  // 盟主修改商家卡包数量
  setApplySingleCount: function() {
    let that = this;
    let leaderSingleCount = that.data.leaderSingleCount;
    if (!leaderSingleCount) {
      wx.showToast({
        icon: 'none',
        title: '请输入发放数量',
        duration: 2000,
        mask: true
      })
      return;
    }
    if (leaderSingleCount > that.data.allianceTotalcount) {
      wx.showToast({
        icon: 'none',
        title: '商家发放数量不能大于联盟总数量',
        duration: 2000,
        mask: true
      })
      return;
    }
    wx.request({
      url: app.globalData.host + '/api/Alliance/SetApplySingleCount?applyId=' + that.data.applyidLeader + '&createUid=' + app.globalData.userInfo.Uid + '&singleCount=' + leaderSingleCount,
      success: function(res) {
        if (res.data.Code == 0) {
          wx.showToast({
            title: '保存成功',
            mask: true,
            duration: 1000
          })
          that.setData({
            showNumber: !that.data.showNumber
          })
          that.getAllianceStaffList(that.data.allianceId);
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  closeShowNumber: function() {
    this.setData({
      showNumber: !this.data.showNumber
    })
  },

  //加入/退出联盟
  setApplyState: function(e) {
    let that = this;
    let state = e.currentTarget.dataset.state;
    let applyid = e.currentTarget.dataset.applyid;
    wx.showModal({
      title: '提示',
      content: '是否退出联盟',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Alliance/SetApplyState?applyId=' + applyid + '&uid=' + app.globalData.userInfo.Uid + '&state=' + state,
            success: function(res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  icon: 'none',
                  title: '退出联盟成功',
                  duration: 2000,
                  mask: true
                })
                that.getAllianceStaffList(that.data.allianceId);
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },
  // 加载更多
  loadMore: function (e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.getAllianceStaffList(that.data.allianceId);
    } else {
      return
    }
  },

    //监听用户滑动页面事件
    onPageScroll: function(e) {
      let top = parseInt(e.scrollTop) + 150;
      this.setData({
        top: top
      })
    },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadMore();
  }
})